/*******************************************************************************
 * SDP Project AUT 2012
 * 
 *                ***********Driving Assistant************
 * Team:
 * Gisela, Deidre, Brijesh, Robert
 * 
 * This class sets up the light panel which displays a visual effect using a 
 * fader to assist alerting the user of an approaching object in front.
 * 
 * @version 1.0
 * 
 * Copyright (c) 2012 by DrivingAssistant Team, Inc., All Rights Reserved.
 * 
 ******************************************************************************/
package com.drivingAssistant.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 
 * Light panel class
 * 
 */
public class Light extends JPanel {
    
    //constants
    private static final int TABLE_WIDTH = 260;    // Pixels.
    private static final int TABLE_HEIGHT = 600;
    private static final String LOGO = "./logo/light.png";
    
    // variables
    private Image image = null;
    JButton startButton, stopButton;
//    ArduinoReader ardReader;
    
    /**
     * Constructor to set up objects for the Logo panel
     */
    public Light() {
        setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));
        try {
            getFileImage(LOGO);
            repaint();
        } catch (Exception ex) {
            System.err.println("Problem finding image file.." + ex);
        }
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        startButton = new JButton("Start");
        startButton.addActionListener(new AbstractAction("Start") {

            @Override
            public void actionPerformed(ActionEvent e) {
//                ardReader = new ArduinoReader();
//                ardReader.start();
            }
        });
        add(startButton, BorderLayout.SOUTH);
        
        stopButton = new JButton("Stop");
        stopButton.addActionListener(new AbstractAction("Stop") {

            @Override
            public void actionPerformed(ActionEvent e) {
//                try {
//                    ardReader = new ArduinoReader();
//                    ardReader.stop();
//                } catch (IOException ex) {
//                    Logger.getLogger(Light.class.getName())
//                            .log(Level.SEVERE, null, ex);
//                }
            }
        });
        add(stopButton, BorderLayout.SOUTH);
    }
    
    /**
     * Get the image
     * 
     * @param filePath which is the directory location of the image
     */
    private void getFileImage(String filePath) {
        
        FileInputStream in = null;
        try {
            in = new FileInputStream(filePath);
            byte[] b = new byte[in.available()];
            in.read(b);
            image = Toolkit.getDefaultToolkit().createImage(b);
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image, 0);
            mt.waitForAll();
        } catch (InterruptedException ex) {
            Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if(in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, 
                        ex);
            }
        }
    }
    
    /**
     * Paint LOGO
     * 
     * @param g which is the graphic variable
     */
    @Override
    public void paintComponent(Graphics g) {
        //... Paint background
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        } else {
            g.drawString("Hello...Imagefile is missing", 40, 40);
        }
    }
    
}
